#import "AppDelegate.h"
#import "GeneratedPluginRegistrant.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
      FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
      FlutterMethodChannel* batteryChannel = [FlutterMethodChannel methodChannelWithName:@"samples.flutter.io/battery" binaryMessenger:controller];

      [batteryChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
          if ([@"getBatteryLevel" isEqualToString:call.method]) {
              int batteryLevel = [self getBatteryLabel];
              if (batteryLevel == -1) {
                  result( [FlutterError errorWithCode:@"UNAVAILABLE" message:@"Battery info unavailable" details:nil]);
              } else {
                  result (@(batteryLevel));
              }
          } else {
              result(FlutterMethodNotImplemented);
          }
      }];
      return [super application:application didFinishLaunchingWithOptions:launchOptions];
  }

  - (int) getBatteryLabel {
      UIDevice* device = [UIDevice currentDevice];
      device.batteryMonitoringEnabled = YES;
      if (device.batteryState == UIDeviceBatteryStateUnknown) {
          return -1;
      } else {
          return (int)(device.batteryLevel * 100);
      }
  }
}

@end
